package com.better.common.exception;

import com.better.common.base.ServiceResponse;
import com.better.common.utils.ServiceResponseHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2017/12/14.
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandle {
    private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

    /**
     * 判断错误是否是已定义的已知错误，不是则由未知错误代替，同时记录在log中
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = DescribeException.class)
    public ServiceResponse exceptionGet(DescribeException e) {
        DescribeException pingjiaException = (DescribeException) e;
        LOGGER.error(e.getMessage());
        return ServiceResponseHandle.failed(pingjiaException.getCode(), pingjiaException.getMessage());
    }
}

